package Solution;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-20
 * Time: 15:50
 */
class Solution2 {
    public String truncateSentence(String s, int k) {
        int sl = s.length();
        int count = 0;
        for (int i = 0; i < sl; i++) {
            if (s.charAt(i) == ' ') {
                count++;
            }
            if (count == k) {
                count = i;
                break;
            }
        }
        if (count < k) {
            count = sl;
        }
        return s.substring(0, count);
    }
}
